﻿// CheckProjectNewDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckProjectNewDialog.h"
#include "afxdialogex.h"


// CCheckProjectNewDialog 对话框

IMPLEMENT_DYNAMIC(CCheckProjectNewDialog, CDialogEx)

CCheckProjectNewDialog::CCheckProjectNewDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_PROJECT_NEW, pParent)
	, m_strProjName(_T(""))
{
	m_pProject = NULL;
}

CCheckProjectNewDialog::~CCheckProjectNewDialog()
{
	m_pProject = NULL;
}

void CCheckProjectNewDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_CHECK_MODEL, m_cmbModels);
	DDX_Control(pDX, IDC_COMBO_CHECK_CASE, m_cmbCases);
	DDX_Text(pDX, IDC_EDIT_PROJECT_NAME, m_strProjName);
}


BEGIN_MESSAGE_MAP(CCheckProjectNewDialog, CDialogEx)
	ON_BN_CLICKED(IDOK, &CCheckProjectNewDialog::OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_COMBO_CHECK_MODEL, &CCheckProjectNewDialog::OnCbnSelchangeComboCheckModel)
END_MESSAGE_MAP()


// CCheckProjectNewDialog 消息处理程序



BOOL CCheckProjectNewDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	PopulateModels();
	

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CCheckProjectNewDialog::PopulateModels()
{
	ck::CheckModel* pModel = NULL;
	ck::EnumCheckModel* pModels = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();

	m_cmbModels.Clear();

	pModels = pModelManager->GetModels();
	pModels->Reset();
	while ((pModel = pModels->Next()))
	{
		z_uint32 code = pModel->GetCode();
		const char* szName = pModel->GetName();
		z_uint32 nItem = m_cmbModels.AddString(pModel->GetName());
		m_cmbModels.SetItemData(nItem, code);
	}
	pModels->Release();
	if (m_cmbModels.GetCount() > 0)
	{
		m_cmbModels.SetCurSel(0);
		z_uint32 iModelCode = m_cmbModels.GetItemData(0);
		PopulateCases(iModelCode);
	}
}

void CCheckProjectNewDialog::PopulateCases(z_uint32 nModelCode)
{
	ck::CheckCase* pCase = NULL;
	ck::EnumCheckCase* pCases = NULL;
	ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();

	m_cmbCases.ResetContent();
	pCases = pCaseManager->GetCases(nModelCode);
	pCases->Reset();
	while ((pCase = pCases->Next()))
	{
		z_uint32 code = pCase->GetCode();
		const char* szName = pCase->GetName();
		z_uint32 nItem = m_cmbCases.AddString(szName);
		m_cmbCases.SetItemData(nItem, code);
	}
	pCases->Release();

	if (m_cmbCases.GetCount() > 0)
	{
		m_cmbCases.SetCurSel(0);
		z_uint32 iCaseCode = m_cmbCases.GetItemData(0);
	}
}

void CCheckProjectNewDialog::OnCbnSelchangeComboCheckModel()
{
	// TODO: 在此添加控件通知处理程序代码
	z_uint32 nItem =  m_cmbModels.GetCurSel();
	if (nItem >= 0)
	{
		z_uint32 nModelCode = m_cmbModels.GetItemData(nItem);
		PopulateCases(nModelCode);
	}

}


void CCheckProjectNewDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	z_uint32 nCaseItem;
	z_uint32 nCaseCode;
	nCaseItem = m_cmbCases.GetCurSel();
	if (nCaseItem < 0)
	{
		AfxMessageBox("未选择检查方案");
		return;
	}

	nCaseCode = m_cmbCases.GetItemData(nCaseItem);
	
	RESULTCODE rc = RC_SUCCESS;
	ck::CheckProject* pProject = NULL;
	ck::CheckProjectManager* pProjectManager = ck::CheckProjectManager::GetInstance();
	rc = pProjectManager->CreateProject(m_strProjName, nCaseCode);
	if (rc != RC_SUCCESS)
	{
		AfxMessageBox("创建工程失败");
		return;
	}

	m_pProject = pProjectManager->GetProject(m_strProjName);

	AfxMessageBox("创建工程成功");

	CDialogEx::OnOK();
}

ck::CheckProject* CCheckProjectNewDialog::GetProject()
{
	return m_pProject;
}